<template>
  <div class="main">
    <!-- 头部导航栏区域 -->
    <van-nav-bar
      right-text="登录"
      left-arrow
      title="盛红房产"
      @click-left="$router.back()"
      @click-right="$router.push('/login')"
    />
    <!-- 头部导航栏区域 -->
    <h3>寓意枝盛叶茂，红遍大江南北</h3>
    <!-- 图片logo区域 -->
    <div class="logo-img">
    <img src="../../assets/logo2.png" />
    </div>
    <!-- 图片logo区域 -->
    <!-- 标题 -->
    <h3>嗨,注册后将开启你的房产之旅！</h3>
    <!-- 标题 -->
    <van-form @submit="onSubmit">
      <div class="include"><van-icon name="manager-o" class="icon icon-user" />
      <van-field
        v-model="user.username"
        name="pattern"
        placeholder="账号"
        :rules="usernameRules"
      >
      </van-field>
      </div>
      <div class="include">
        <van-icon name="envelop-o" class="icon icon-email" />
                          <van-field
        v-model="user.email"
        name="pattern"
        placeholder="邮箱"
        :rules="[{ required: true, message: '请输入邮箱' }, { pattern, message: '请输入正确的邮箱' }]"
        class="van-field-password"
      >
        </van-field>
      </div>
<div class="include">
<van-icon name="goods-collect-o" class="icon icon-password" />
  <van-field
        v-model="user.password"
        type="password"
        name="pattern"
        placeholder="密码"
        class="van-field-password"
        :rules="passwordRules"
      ></van-field></div>

      <div style="margin: 16px">
        <van-button round block type="info" native-type="submit" color="#4ec7fe"
          >注册</van-button
        >
      </div>
    </van-form>
  </div>
</template>

<script>
import { register } from '@/api/user'
export default {
  name: 'RegisterIndex',

  data () {
    return {
      user: {
        username: '',
        email: '',
        password: ''
      },
      pattern: /[a-zA-Z0-9]+([-_.][A-Za-zd]+)*@([a-zA-Z0-9]+[-.])+[A-Za-zd]{2,5}$/,
      usernameRules: [
        { required: true, message: '请填写用户名' }
      ],
      passwordRules: [{ required: true, message: '请填写密码' }]
    }
  },
  mounted () {
    const loginForm = document.querySelector('form')
    const loginInput = loginForm.querySelectorAll('input')[0]
    const eamilInput = loginForm.querySelectorAll('input')[1]
    loginInput.autocomplete = 'off'
    eamilInput.autocomplete = 'off'
    loginInput.focus()
  },

  methods: {
    // 表单提交事件处理函数
    async onSubmit () {
      try {
        await register(this.user)
        this.$toast('注册成功！')
        setTimeout(() => { this.$router.push('/login') }, 1000)
      } catch (error) {

      }
    }
  }
}
</script>

<style lang="scss" scoped>
$textColor: #4ec7fe;
.main {
  h3,h5{
    margin: 0;
  }
  ::v-deep .van-nav-bar {
    .van-icon,
    .van-nav-bar__text {
      color: #4ec7fe;
    }
        .van-nav-bar__title{
      color: #4ec7fe;
      font-size: 21px;
    }
  }
  .logo-img{
    margin-top: 30px;
        display: flex;
    justify-content: center;
  }
  img {
    width: 80%;
  }
  h3 {
    text-align: center;
    font-weight: normal;
    margin: 25px 0;
    color: $textColor;
  }
  ::v-deep .van-form {
    position: relative;
    .van-field__control {
      padding-left: 28px;
    }
    .van-field,.include {
      position: relative;
      width: 70vw;
      margin: 0 auto;
      background-color: #f7f8fa;
      border-radius: 25px;
    }
    .van-field-password,.van-field-password {
      margin-top: 25px;
    }
    .van-button {
      margin: 0 auto;
      margin-top: 35px;
      width: 70vw;
    }
    .icon {
      z-index: 9999;
      position: absolute;
      left: 5%;
      font-size: 25px;
      color: $textColor;
    }
    .icon-user {
      top: 16%;
    }
    .icon-password {
      top: 23%;
    }
    .icon-email{
      top: 24%;
    }
  }
}
</style>
